﻿var LaddaArray = new Array();
var useOneArray = new Array();
var Global = {
		
	 alert: function (msg) {
	    	var msgshow = msg;
	    	if(msgshow == null){
	    		msgshow="操作成功，返回";
	    	}
	        //提示框
	        top.bootbox.alert({
	            buttons: {
	                ok: {
	                    label: '返回',
	                    className: 'btn-myStyle'
	                }
	            },
	            message: msgshow,
	        });
	 },
	 alertIn: function (msg) {
	    	var msgshow = msg;
	    	if(msgshow == null){
	    		msgshow="操作成功，返回";
	    	}
	        //提示框
	        bootbox.alert({
	            buttons: {
	                ok: {
	                    label: '返回',
	                    className: 'btn-myStyle'
	                }
	            },
	            message: msgshow,
	        });
	 },
		
		
	 alertWithCallback: function (msg,callback) {
	    	var msgshow = msg;
	    	if(msgshow == null){
	    		msgshow="操作成功，返回";
	    	}
	        //提示框
	        top.bootbox.alert({
	            buttons: {
	                ok: {
	                    label: '返回',
	                    className: 'btn-myStyle'
	                }
	            },
	            callback: function () {
	                callback();
	            },
	            message: msgshow,
	        });
	 },
	 alertWithCallbackIn: function (msg,callback) {
	    	var msgshow = msg;
	    	if(msgshow == null){
	    		msgshow="操作成功，返回";
	    	}
	        //提示框
	        bootbox.alert({
	            buttons: {
	                ok: {
	                    label: '返回',
	                    className: 'btn-myStyle'
	                }
	            },
	            callback: function () {
	                callback();
	            },
	            message: msgshow,
	        });
	 },
	 //弹出确认选择框，选择确定后，执行callback函数
	 alertConfirm:function(title,labCancel,labOK,callback){
		 top.bootbox.confirm({
             buttons: {
                 cancel: {
                     label: labCancel,
                     className: ''
                 },
                 confirm: {
                     label: labOK,
                     className: ''
                 }
             },
             message: title,
             callback: function (isok) {
            	 if(isok){
                	 callback();
            	 }
             }
         });
	 },
	 alertError: function (data) {
	    	
	 },
	 hudAdd:function(target){
		 	

			$("#huddiv").empty();
			var hudDIV = $("<div class='huddivclass' id='huddiv'><h1 class='hudcenter'><i class='ace-icon fa fa-spinner fa-spin orange bigger-125'></i><h1></div>");
			target.append(hudDIV);
	 },
	 hudRemove:function(){
			var div = document.getElementById("huddiv");//选择div元素
  			 div.parentNode.removeChild(div);
	 },

     showBtnHud:function(btnName,isShow){
    	//加入Ladda相关属性样式
    	var jqobj = $("#"+btnName); 
    	jqobj.addClass("ladda-button"); 
    	jqobj.attr("data-style","expand-up");//data-style=""  data-size="l"
    	jqobj.attr("data-size","l");
    	 
		var targetLadda = LaddaArray[btnName];
		if(targetLadda==null){
			targetLadda = Ladda.create( document.querySelector( '#'+btnName ) );
	    	LaddaArray[btnName] = targetLadda;
		}
		if(isShow){
			targetLadda.start();
		}else{
			targetLadda.stop();
		}
	},
	
	trim :function(str){
		return str.replace(/(^\s*)|(\s*$)/g, "");
	},
	triml :function(str){
		return str.replace(/(^\s*)/g,"");
	},
	trimr :function(str){
		return str.replace(/(\s*$)/g,"");
	},
	post:function(postUrl,keys,values){
		
		if(keys.length != values.length){
			alert("key value数量不符合");
			return;
		}
	    // 取得要提交页面的URL  
	    var action = postUrl;  
	    // 创建Form  
	    var form = $('<form></form>');  
	    // 设置属性  
	    form.attr('action', action);  
	    form.attr('method', 'post');  
	    form.css("display","none");
	    // form的target属性决定form在哪个页面提交  
	    // _self -> 当前页面 _blank -> 新页面  
	    //form.attr('target', '_self');  
	    // 创建Input  
	    for(var i=0;i<keys.length;i++){
	    	var key = keys[i];
	    	var val = values[i];
	    	
		    var inputInstance = $("<input type='text' name='"+key+"' />");  
		    inputInstance.attr('value', val);  
		    // 附加到Form  
		    form.append(inputInstance);  
	    }

	    $(document.body).append(form);
	    // 提交表单  
	    form.submit();  
	    // 注意return false取消链接的默认动作  
	    return false;
	},
	setJsCheckAction:function(idName,isCheck){
		$(idName).get(0).checked = isCheck;
		if(isCheck){
			$(idName).val(1);
		}else{
			$(idName).val(0);
		}
	}, 
	setJsSelectionAction:function(idName,index){
		$(idName).val(index);
		//alert($(idName).val());
	},
	showErrorTip:function(containerJqueryObj,showLabel){
		$("#alertErrorTip").remove();
		var tipDiv = "<div id='alertErrorTip' onclick='$(this).remove();' class='alert alert-danger'><button type='button' class='close' data-dismiss='alert'><i class='ace-icon fa fa-times'></i></button><strong><i class='ace-icon fa fa-times'></i>&nbsp;&nbsp;Error &nbsp;&nbsp;:&nbsp;&nbsp; </strong>" +
		showLabel+"<br /></div>";
		containerJqueryObj.prepend(tipDiv);
		//setTimeout(function(){$("#alertErrorTip").remove();}, 3000);
	},
	// 在提交表单之前调用checkBoxFilterSelectToPost这个方方法，在提交之后调用checkBoxFilterSelectReset恢复状态
	checkBoxFilterSelectToPost:function(){
		$("input[type='checkbox']").each(function(){
			if (!$(this).is(":checked")) {
				$(this)[0].checked=true;
				$(this)[0].value="0";
				useOneArray.push($(this)[0]);
		    }
		});
	},
	checkBoxFilterSelectReset:function(){
		for(var i=0;i<useOneArray.length;i++){
			useOneArray[i].checked = false;
		}
	},
	isJson:function(obj){  
	    var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;   
	    return isjson;  
	},
	isNum:function(val){
	    var regPos = /^\d+$/; // 非负整数
	    var regNeg = /^\-[1-9][0-9]*$/; // 负整数
	    if(regPos.test(val) || regNeg.test(val)){
	        return true;
	    }else{
	        return false;
	    }
	},	
	strongPass:function checkPass(pass){
		//返回<3的话，说明为弱密码
		if(pass.length < 8){  return 0; } 
		var ls = 0; 
		if(pass.match(/([a-z])+/)){  ls++; }  
		if(pass.match(/([0-9])+/)){  ls++; }  
		if(pass.match(/([A-Z])+/)){   ls++; }  
		return ls; 
	},
	contains:function (array,val)  
	{  
	     for (var i = 0; i < array.length; i++)  
	    {  
	       if (array[i] == val)  
	      {  
	       return true;  
	      }  
	    }  
	     return false;  
	},
	
	//校验邮箱
	validEmail:function(email){
		if((/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ .test(email))){
			return true;
		}else{
			return false;
		}
	},
	//校验手机
	validTelphone:function(telphone){
		if(!(/^1[0-9][0-9]\d{8}$/.test(telphone))){
			return true;
		}else{
			return false;
		}
	},
	//校验手机
	validAge:function(age){
		if(!(/^[0-9]{1,3}$/.test(age))){
			return true;
		}else{
			return false;
		}
	}
}
